import type { Directive } from 'vue'

export const vIntersection: Directive = {
  mounted(el, binding) {
    const observer = new IntersectionObserver(
      (entries) => {
        if (entries[0].isIntersecting) {
          binding.value()
        }
      },
      {
        threshold: 0.5
      }
    )
    observer.observe(el)
  },
  unmounted(el) {
    // 清理观察器
    const observer = new IntersectionObserver(() => {})
    observer.unobserve(el)
    observer.disconnect()
  }
}
